//
//  LBMediaPresentViewController.m
//  LangBa
//
//  Created by Mac on 2017/12/21.
//  Copyright © 2017年 stoneobs@icloud.com. All rights reserved.
//

#import "BBClearPresentViewController.h"
#import "UIImage+STTools.h"
@interface BBClearPresentViewController ()
@property(nonatomic, strong) STButton                     *clearButton;
@property(nonatomic, strong) UIView                     *customView;
@property(nonatomic, strong) UIImage                     *image;
@property(nonatomic, strong) NSMutableArray                     *buttonsArray;

@end

@implementation BBClearPresentViewController

- (instancetype)init
{
    if (self == [super init]) {
        self.modalPresentationStyle = UIModalPresentationOverFullScreen;
    }
    return self;
}
#pragma mark --vc 生命周期
- (void)viewDidLoad{
    [super viewDidLoad];
    [self configSubView];
}
- (void)viewWillAppear:(BOOL)animated{
    [super viewWillAppear:animated];
}
- (void)viewDidAppear:(BOOL)animated{
    [super viewDidAppear:animated];
    [self show];
}
- (void)viewWillDisappear:(BOOL)animated{
    [super viewWillDisappear:animated];
}
- (void)viewDidDisappear:(BOOL)animated{
    [super viewDidDisappear:animated];
}
#pragma mark --subView
- (void)configSubView{
    __weak typeof(self) weakSelf =  self;
    self.clearButton = [[STButton alloc] initWithFrame:UIScreenFrame
                                                 title:nil
                                            titleColor:nil
                                             titleFont:0
                                          cornerRadius:0
                                       backgroundColor:[[UIColor blackColor] colorWithAlphaComponent:0.4]
                                       backgroundImage:nil
                                                 image:nil];
    [weakSelf.clearButton setClicAction:^(UIButton *sender) {
        [weakSelf dismiss];
    }];
    [self.view addSubview:_clearButton];
    
    self.customView = [[UIView alloc] initWithFrame:CGRectMake(40, UIScreenHeight, UIScreenWidth - 80, 342)];
    self.customView.centerX = UIScreenWidth / 2;
    self.customView.top = UIScreenHeight;
    self.customView.backgroundColor = [UIColor whiteColor];
    [self.clearButton addSubview:self.customView];
    UITapGestureRecognizer * ges = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(onSelectedWhiteView)];
    self.customView.userInteractionEnabled = YES;
    [self.customView addGestureRecognizer:ges];
    
    BBUserModel * user = [STUserManger defult].loginedUser;
    //sub
    
    STButton * buyButton = [[STButton alloc] initWithFrame:CGRectMake(0, 0, self.customView.width, 44)
                                                     title:@"清除记录"
                                                titleColor:FirstTextColor
                                                 titleFont:15
                                              cornerRadius:0
                                           backgroundColor:FlatGray
                                           backgroundImage:nil
                                                     image:[UIImage imageNamed:@"设置1"]];
    buyButton.imageEdgeInsets = UIEdgeInsetsMake(0, 15, 0, 0);
    buyButton.titleEdgeInsets = UIEdgeInsetsMake(0, 25, 0, 0);
    [self.customView addSubview:buyButton];
    
    
    
    NSArray * titles = @[@"收藏历史",@"搜索历史",@"浏览历史",@"缓存文件",
                         @"Cookies",@"首页数据",@"表单密码",@"Flash缓存",
                         @"位置授权"];
    CGFloat menuWith = self.customView.width / 2 - 30;
    CGFloat bootom = 0;
    self.buttonsArray = [NSMutableArray new];
    for (NSInteger i = 0; i < titles.count; i ++) {
        NSString * title = titles[i];
        STButton * menuButton = [[STButton alloc] initWithFrame:CGRectMake(0, 20, menuWith, 30)
                                                         title:title
                                                    titleColor:FirstTextColor
                                                     titleFont:15
                                                  cornerRadius:0
                                               backgroundColor:nil
                                               backgroundImage:nil
                                                         image:[UIImage imageNamed:@"icon_勾选_未选中"]];
        menuButton.contentHorizontalAlignment = UIControlContentHorizontalAlignmentLeft;
        [menuButton setImage:[UIImage imageNamed:@"选择统计"]
                   forState:UIControlStateSelected];
        [self.customView addSubview:menuButton];
        [menuButton setClicAction:^(UIButton *sender) {
            sender.selected = !sender.selected;
        }];
        menuButton.left = 15 + 15 * (i%2) +(i%2)*menuWith ;
        menuButton.top = 44 + i/2 * 30;
        bootom = menuButton.bottom;
        [self.buttonsArray addObject:menuButton];
        if (i == 0 || i == 1  || i == 2 || i == 7) {
            menuButton.selected = YES;
        }
    }

    
    
    STButton * cancelButton = [[STButton alloc] initWithFrame:CGRectMake(0, bootom + 20, 80, 44)
                                                        title:@"取消"
                                                   titleColor:[UIColor whiteColor]
                                                    titleFont:14
                                                 cornerRadius:10
                                              backgroundColor:TM_ThemeBackGroundColor
                                              backgroundImage:nil
                                                        image:nil];
    cancelButton.contentHorizontalAlignment = UIControlContentHorizontalAlignmentCenter;
    [cancelButton setClicAction:^(UIButton *sender) {
        [weakSelf dismissWithHandle:^{
            [weakSelf onSelctedConfimButton];
        }];
    }];
    cancelButton.right = self.customView.width / 2 - 20;
    [self.customView addSubview:cancelButton];
    
    STButton * confimButton = [[STButton alloc] initWithFrame:CGRectMake(0, bootom + 20, 80, 44)
                                                        title:@"确认"
                                                   titleColor:[UIColor whiteColor]
                                                    titleFont:14
                                                 cornerRadius:10
                                              backgroundColor:TM_ThemeBackGroundColor
                                              backgroundImage:nil
                                                        image:nil];
    confimButton.contentHorizontalAlignment = UIControlContentHorizontalAlignmentCenter;
    [confimButton setClicAction:^(UIButton *sender) {
        [weakSelf dismissWithHandle:^{
            if (weakSelf.chosedMenuHandle) {
                weakSelf.chosedMenuHandle([weakSelf arrayFormButtonSelcted]);
            }
        }];
    }];
    confimButton.left = self.customView.width / 2 + 20;
    [self.customView addSubview:confimButton];
    
    
    
    
    
    self.customView.height = cancelButton.bottom + 20;
    self.customView.top = UIScreenHeight;
    
    
}
- (NSArray*)arrayFormButtonSelcted{
    NSMutableArray * array = [NSMutableArray new];
    for (UIButton * button in self.buttonsArray) {
        if (button.selected) {
            [array addObject:button.currentTitle];
        }
    }
    return [array copy];
}
#pragma mark --Private Method
- (void)show{
    [UIView animateWithDuration:0.6
                          delay:0
         usingSpringWithDamping:0.6
          initialSpringVelocity:0.7
                        options:UIViewAnimationOptionLayoutSubviews
                     animations:^{
                         self.customView.centerY = UIScreenHeight / 2;
                     } completion:^(BOOL finished) {
                         
                     }];
}
- (void)dismiss{
    [self.view endEditing:YES];
    [UIView animateWithDuration:0.25 animations:^{
        self.customView.top = UIScreenHeight;
        self.clearButton.alpha = 0;
    } completion:^(BOOL finished) {
        [self dismissViewControllerAnimated:NO completion:nil];
    }];
}
- (void)dismissWithHandle:(void(^)())handle{
    [self.view endEditing:YES];
    [UIView animateWithDuration:0.25 animations:^{
        self.customView.top = UIScreenHeight;
        self.clearButton.alpha = 0;
    } completion:^(BOOL finished) {
        [self dismissViewControllerAnimated:NO completion:^{
            if (handle) {
                handle();
            }
        }];
    }];
}
#pragma mark --Action Method

- (void)onSelectedCancleButton{
    
    [self dismiss];
    
}
- (void)onSelectedWhiteView{
    [self.view endEditing:YES];
}
- (void)onSelctedConfimButton{
    
}
#pragma mark --NetWork Method
- (void)sendQrcodeRequest{
    
}

@end






